package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.QueryObject.TravelCommentQuery;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.respository.TravelCommentRespository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时不管事务
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRespository travelCommentrRespository;

    @Autowired
    private IDestinationService destinationService;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public TravelComment get(String id) {
        Optional<TravelComment> optional = travelCommentrRespository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    @Override
    public List<TravelComment> list() {
        return travelCommentrRespository.findAll();
    }

    @Override
    public void addComment(TravelComment travelComment) {
        if (travelComment == null) {
            throw new LogicException("添加游记评论失败");
        }
        //创建时间
        travelComment.setCreateTime(new Date());
        travelComment.setId(null);
        //获取引用评论 判断是否存在
        String rid = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(rid)) {
            //若引用评论存在 维护好引入评论
            TravelComment refComment = this.get(rid);//获取引用评论
            travelComment.setRefComment(refComment);
        }
        travelCommentrRespository.save(travelComment);
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getTravelId())) {
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }
        return DBHelper.query(mongoTemplate, TravelComment.class, query, qo.getPageable());
    }

    @Override
    public void commentThumb(String cid, String uid) {
        /*//判断是点赞还是取消点赞
        //1.调用上面的方法根据id获取评论
        TravelComment travelComment = this.get(cid);
        //2.获取评论中的点赞人集合
        List<String> thumbuplist = travelComment.getThumbuplist();
        //3.判断点赞还是取消点赞
        if (thumbuplist.contains(uid)) {
            //包含 即已点过赞
            thumbuplist.remove(uid);
            travelComment.setThumbupnum(travelComment.getThumbupnum() - 1);
        } else {
            //不包含 即未过赞
            thumbuplist.add(uid);
            travelComment.setThumbupnum(travelComment.getThumbupnum() + 1);
        }
        //4.更新
        travelCommentrRespository.save(travelComment);*/
    }

}
